package com.search.controller;

import com.search.bturls.Bturls;
import com.search.bturls.BturlsService;
import com.search.commons.CommonPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by yijy on 2017/6/30.
 */
@Controller
@RequestMapping("/")
public class MainController {
    @Autowired
    BturlsService bturlsService;
    @RequestMapping("main")
    public ModelAndView gotoMainPage()
    {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("hello","hello world");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping("search")
    public ModelAndView goSearch(String searchValue)
    {
        ModelAndView modelAndView = new ModelAndView();
        CommonPage<Bturls> str = bturlsService.getBtDataList(searchValue,1);
        List<Bturls> list = str.getDataList();
        modelAndView.addObject("resultList",list);
        modelAndView.addObject("searchValue",searchValue);
        modelAndView.addObject("pageCount",str.getPageCount());
        modelAndView.addObject("currentCount",str.getCurrentCount());
        modelAndView.addObject("dataList",str.getDataList());
        modelAndView.setViewName("result");
        return modelAndView;
    }

    @RequestMapping("goMagnet")
    public ModelAndView goMagnet(String url)
    {
        ModelAndView modelAndView = new ModelAndView();
        //List<Bturls> bturlsList = bturlsService.getBtDetail(url);
       // modelAndView.addObject("bturlsList",bturlsList);
        modelAndView.setViewName("detail");
        return modelAndView;
    }

    @RequestMapping("turnPage")
    public ModelAndView turnPage(String searchValue,String currentCount)
    {
        ModelAndView modelAndView = new ModelAndView();
        int count = Integer.parseInt(currentCount);
        CommonPage<Bturls> str = bturlsService.getBtDataList(searchValue,count);
        List list = str.getDataList();
        modelAndView.addObject("resultList",list);
        modelAndView.addObject("searchValue",searchValue);
        modelAndView.addObject("pageCount",str.getPageCount());
        modelAndView.addObject("currentCount",str.getCurrentCount());
        modelAndView.addObject("dataList",str.getDataList());
        modelAndView.setViewName("result");
        return modelAndView;
    }



}
